package cn.ithers.blog.controller.admin;

import cn.ithers.blog.annotion.CheckTooFrequentCommit;
import cn.ithers.blog.dto.LooperDTO;
import cn.ithers.blog.pojo.Looper;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.ILoopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhanghuan
 * @date: 2020/8/28 14:06
 * @description:
 */
@Api(value = "轮播图", tags = "轮播图")
@RestController
@RequestMapping("/admin/loop")
public class LooperAdminApi {

    @Autowired
    private ILoopService loopService;

    /**
     * 添加轮播图
     *
     * @param looperDTO
     * @return
     */
    @CheckTooFrequentCommit
    @ApiOperation("添加轮播图")
    @PreAuthorize("@permission.admin()")
    @PostMapping()
    public ResponseResult addLoop(@RequestBody LooperDTO looperDTO) {
        return loopService.addLoop(looperDTO);
    }

    /**
     * 删除轮播图
     *
     * @param loopId
     * @return
     */
    @ApiOperation("删除轮播图")
    @PreAuthorize("@permission.admin()")
    @DeleteMapping("/{loopId}")
    public ResponseResult deleteLoop(@ApiParam(value = "轮播图ID", required = true) @PathVariable("loopId") String loopId) {
        return loopService.deleteLoop(loopId);
    }

    /**
     * 修改轮播图
     *
     * @param loopId
     * @return
     */
    @CheckTooFrequentCommit
    @ApiOperation("修改轮播图")
    @PreAuthorize("@permission.admin()")
    @PutMapping("/{loopId}")
    public ResponseResult updateLoop(@ApiParam(value = "轮播图ID", required = true) @PathVariable("loopId") String loopId,
                                     @RequestBody LooperDTO looperDTO) {
        return loopService.updateLoop(loopId, looperDTO);
    }

    /**
     * 获取轮播图
     *
     * @param loopId
     * @return
     */
    @ApiOperation("获取轮播图")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/{loopId}")
    public ResponseResult getLoop(@ApiParam(value = "轮播图ID", required = true) @PathVariable("loopId") String loopId) {
        return loopService.getLoop(loopId);
    }

    /**
     * 获取轮播图列表list
     *
     * @return
     */
    @ApiOperation("获取轮播图列表list")
    @PreAuthorize("@permission.admin()")
    @GetMapping("/list}")
    public ResponseResult listLoops() {
        return loopService.loopsList();
    }

}
